function f_check_disk_space()
{ #检查主库的磁盘空间剩余大小, 要求满足两倍于当前表大小
    declare mysql_local_free_size space_is_ok fun_name
    fun_name="f_check_disk_space"
    [ "${no_check_disk}x" == "falsex" ] && return 0
    mysql_local_free_size="$(f_exe_ssh_comm "${mysql_host}" "df"|grep "${mysql_data_mount_dir}$")" || true
    mysql_local_free_size="$(awk '{if ($4/1024>0) {printf( "%.f\n",$4/1024)} else{print 0}}' <<< "${mysql_local_free_size}")" || true
    if [ -z "${mysql_local_free_size}" ] || [ "$(echo "${mysql_local_free_size}"|sed 's/[0-9.]//g')x" == "0x" ]
    then
        f_logging "ERROR:${fun_name}" "f_exe_ssh_comm "${mysql_host}" "df" 获取'${mysql_host}'机器剩余空间大小异常,请检查" "2" "1"
    fi

    #判断空间 要求剩余空间大于两倍表大小, disk_space_mark对应${dbadb_port} ${dbadb_db} dbadb_alter_table_repl_time 表的disk_space字段的值
    space_is_ok="$(awk '{if ($1 > $2 * 2) {print 1}else if ($1 > $2 + 51200){print 2}else {print 0}}' <<< "${mysql_local_free_size} ${table_size}")" 

    if [ "${space_is_ok}x" == "2x" ]
    then #space_is_ok=2表示不满足两倍
        if [ "${disk_space_mark}x" == "truex" ]
        then
            f_logging "ERROR:${fun_name}" "'${mysql_host}' 机器剩余空间大小为'${mysql_local_free_size}MB', 不满足两倍表大小, 但是使用了'-S true'" "execute:${execute}"
        else
            return__="'${mysql_host}' 机器剩余空间大小 '${mysql_local_free_size}MB'. 要求要大于两倍表大小, 不满足改表操作. "
            return__="${return__} 如果确认当前主库机器剩余空间满足本次改表操作, 请联系dba处理(在管理机执行成功后让用户重新提交"
            return__="${return__}  : main.sh -a disk_space -P ${mysql_port})"
            return 1
        fi
    elif [ "${space_is_ok}x" == "0x" ]
    then #space_is_ok=0表示不满足两倍且不满足【一倍+50G】
        return__="获取到 '${mysql_host}' 机器剩余空间大小 '${mysql_local_free_size}MB'. 要求要大于两倍表大小, 不满足改表操作. 请联系dba"
        return 1
    fi
    f_logging "REMAIND:${fun_name}" "获取到 '${mysql_host}' 机器剩余空间大小 '${mysql_local_free_size}MB'" "execute:${execute}"
}

function f_check_disk_for_node()
{ #检查节点的磁盘空间, 如果使用率大于85%就清理binlog, 主要是避免改变产生大量binlog导致磁盘空间告警
    declare _disk_info _free_disk _free_disk_mark _use_per _use_per_mark _check_mysql_node _sec_binlog fun_name
    fun_name="f_check_disk_for_node"
    [ "${no_check_disk}x" == "falsex" ] && return 0
    [ -z "${print_check_disk_info}" ] && f_logging "REMAIND:${fun_name}" "开始检查${mysql_host} $(sed -r 's/:[^,]+,/ /g' <<< "${mysql_slave_info[@]%:*}")磁盘空间使用率"
    print_check_disk_info=0 #无用变量, 仅用于打印上面这个提示信息
    for _check_mysql_node in $(tr "," " " <<< "${mysql_host},${mysql_slave_info[@]}")
    do 
        [ "${no_check_disk}x" == "flasex" -a "${_check_mysql_node}x" == "${mysql_host}x" ] && continue   #忽略检查磁盘空间
        _check_mysql_node="$(awk -F: '{print $1}' <<< "${_check_mysql_node}")"
        f_check_ssh_auth "${_check_mysql_node}" 2>/dev/null || continue
        while :
        do
            sleep 1
            [ ! -f "${osc_run_mark_file}" ] && break
            _disk_info="$(f_exe_ssh_comm "${_check_mysql_node}" "df"|grep "${mysql_data_mount_dir}$")" || true

            _free_disk="$(awk -F' ' 'OFS=" ",NF-=2{print $NF}' <<< "${_disk_info}")" || true
            _free_disk_mark="$(awk -v num="${free_disk}" '{if ($1<num){print "error"}else if($1>0){print "ok"}}' <<< "${_free_disk}")" || true

            _use_per="$(awk -F' ' 'OFS=" ",NF-=1{print $NF}' <<< "${_disk_info}")" || true
            _use_per_mark="$(awk -v num="${disk_alarm}" -F'%' '{
                                                                   if ($1>=num+7){print "fault_error"} #磁盘使用率, 超过92%是严重错误
                                                                   else if($1>num+5){print "error"}    #磁盘使用率, 超过90%是错误
                                                                   else if($1>num){print "warn"}       #磁盘使用率, 超过85%是告警
                                                                   else{print "ok"}}' <<< "${_use_per}")" || true
            if [ -z "${_disk_info}" ] || [ -z "${_free_disk}" ] || [ -z "${_free_disk_mark}" ]
            then #任何值为空都是异常的
                f_logging "ERROR:${fun_name}" "获取 '${_check_mysql_node}' 机器磁盘空间使用情况异常, 请检查" && break
            fi

            if [ "${_use_per_mark}x" == "fault_errorx" ]
            then
                f_logging "ERROR:${fun_name}" "${_check_mysql_node}'机器磁盘使用率[${_use_per}]超过92%,现在终止改表操作,如有疑问请联系dba"
                f_stop_task_for_alter
            elif [ "${_use_per_mark}x" == "warnx" -a "${_free_disk_mark}x" == "errorx" ] || [ "${_use_per_mark}x" == "errorx" ]
            then #使用率大于85%且不足大小为300g, 或者使用率大于90%
                _sec_binlog="$(f_exe_sql_in_mysql "show binary logs" "${_check_mysql_node}" "${mysql_port}" "slave"|head -2|tail -1|awk '{print $1}')"
                _count_binlog="$(f_exe_sql_in_mysql "show binary logs" "${_check_mysql_node}" "${mysql_port}" "slave"|wc -l|awk '{if ($1>=50){print "ok"}}')"
                [ -z "${_sec_binlog}" ] && break || sleep 1   #binlog文件获取失败
                _fir_binlog="$(awk -F'.' '{printf ("%s.%06d\n",$1,$2-1)}' <<< "${_sec_binlog}")"
                if [ "${_count_binlog}x" == "okx" ]
                then #binlog文件个数要求大于50个
                    f_logging "ERROR:${fun_name}" "'${_check_mysql_node}' 磁盘使用率[${_use_per}]超过${disk_alarm}%, 现在开始清理binlog[${_fir_binlog}"
                    f_exe_sql_in_mysql "purge binary logs to '${_sec_binlog}';" "${_check_mysql_node}" "${mysql_port}" "slave" && continue || break
                else
                    f_logging "ERROR:${fun_name}" "'${_check_mysql_node}' 磁盘使用率[${_use_per}]超过${disk_alarm}%, 应该清理binlog, 但是binlog文件不够50个, 清理失败"
                    break
                fi
            fi
            break
        done
    done
    return 0
}

